package top.codecrab.eblog.config;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.codecrab.eblog.common.properties.EblogProperties;
import top.codecrab.eblog.oauth2.entity.OAuth2QQ;
import top.codecrab.eblog.oauth2.entity.OAuth2WeiBo;
import top.codecrab.eblog.oauth2.properties.OAuth2Properties;

import java.util.List;

/**
 * @author codecrab
 */
@Data
@Component
public class Constants implements InitializingBean {

    @Autowired
    private OAuth2Properties oAuth2Properties;

    @Autowired
    private EblogProperties eblogProperties;

    public static String UPLOAD_DIR;

    public static String EBLOG_HOST;

    public static final String AI_TE = "@";

    public static final String STICK = "stick";
    public static final String STATUS = "status";
    public static final String DELETE = "delete";

    public static final String AUTO_LOGIN_TAG_PREFIX = "^$";
    public static final String AUTO_LOGIN_TAG = AUTO_LOGIN_TAG_PREFIX + "isEncode";

    public static List<String> ADMIN_LIST;

    public static String GLOBAL_REDIS_PREFIX = "eblog:";
    public static boolean GLOBAL_REDIS_PREFIX_ENABLE = false;

    public static final Long IM_DEFAULT_USER_ID = 999L;

    public final static Long IM_GROUP_ID = 999L;
    public final static String IM_GROUP_NAME = "e-group-study";
    public static final String GENDER_MAN = "0";
    public static final String GENDER_WOMAN = "-1";

    /** 消息类型 */
    public final static String IM_MESS_TYPE_PING = "pingMessage";
    public final static String IM_MESS_TYPE_CHAT = "chatMessage";

    public static final String IM_ONLINE_MEMBERS_KEY = "online_members_key";
    public static final String IM_GROUP_HISTROY_MSG_KEY = "chat:group:history_msg_key";

    /** 微博 */
    public static String WEIBO_OAUTH2;
    public static String WEIBO_REDIRECT_URI;
    public static final String WEIBO_BIND_CODE = "bindCode:weibo:";
    public static final String WEIBO_ACCESS_TOKEN = "https://api.weibo.com/oauth2/access_token";
    public static final String WEIBO_USER_SHOW = "https://api.weibo.com/2/users/show.json";
    public static final String WEIBO_LOGOUT = "https://api.weibo.com/oauth2/revokeoauth2";
    public static final String WEIBO_OAUTH2_URL_TEM = "https://api.weibo.com/oauth2/authorize?client_id={}&response_type=code&redirect_uri={}";
    public static final String WEIBO_OAUTH2_URL_BIND_TEM = "https://api.weibo.com/oauth2/authorize?client_id={}&state={}&response_type=code&redirect_uri={}";

    /** QQ */
    public static String QQ_OAUTH2;
    public static String QQ_REDIRECT_URI;
    public static final String QQ_BIND_CODE = "bindCode:qq:";
    public static final String QQ_ACCESS_TOKEN = "https://graph.qq.com/oauth2.0/token";
    public static final String QQ_USER_OPEN_ID = "https://graph.qq.com/oauth2.0/me";
    public static final String QQ_USER_SHOW = "https://graph.qq.com/user/get_user_info";
    public static final String QQ_OAUTH2_URL_TEM = "https://graph.qq.com/oauth2.0/authorize?client_id={}&state={}&response_type=code&redirect_uri={}";

    /**
     * 当私有成员被赋值后，此方法自动被调用，从而初始化常量
     */
    @Override
    public void afterPropertiesSet() {
        OAuth2QQ qq = oAuth2Properties.getQq();
        OAuth2WeiBo weibo = oAuth2Properties.getWeibo();

        EBLOG_HOST = eblogProperties.getHost();
        UPLOAD_DIR = eblogProperties.getUploadDir();
        ADMIN_LIST = StrUtil.split(eblogProperties.getAdminList(), ",");
        GLOBAL_REDIS_PREFIX = eblogProperties.getGlobalRedisPrefix();
        GLOBAL_REDIS_PREFIX_ENABLE = eblogProperties.getGlobalRedisPrefixEnable();
        //微博请求地址初始化
        WEIBO_REDIRECT_URI = URLUtil.encode(weibo.getRedirectUri());
        WEIBO_OAUTH2 = StrUtil.format(WEIBO_OAUTH2_URL_TEM, weibo.getClientId(), WEIBO_REDIRECT_URI);
        //QQ请求地址初始化
        QQ_REDIRECT_URI = URLUtil.encode(qq.getRedirectUri());
        QQ_OAUTH2 = StrUtil.format(QQ_OAUTH2_URL_TEM, qq.getClientId(), "login", QQ_REDIRECT_URI);
    }
}
